<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input
            placeholder="请输入商品名称"
            v-model="queryinfo.query"
            size="small"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="query()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click="gosave()"
            >添加</el-button
          >
        </el-col>
      </el-row>
      <el-row>
        <el-table class="tabletop" :data="tableData" border style="width: 100%">
          <el-table-column type="index" label="序号"></el-table-column>
          <el-table-column
            prop="goods_name"
            label="商品名称"
            width="300"
          ></el-table-column>
          <el-table-column
            prop="goods_price"
            label="商品价格"
          ></el-table-column>
          <el-table-column
            prop="goods_number"
            label="商品数量"
          ></el-table-column>
          <el-table-column label="创建时间">
            <template slot-scope="scope" width="360">
              <!-- 过滤器在指令中的使用方式 -->
              <div v-html="$options.filters.fmdtime(scope.row.add_time)"></div>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="150">
            <template slot-scope="{ row }">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="goeait(row)"
              ></el-button>
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="del(row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryinfo.pagesize"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalsize"
        ></el-pagination>
      </el-row>
    </el-card>
    <!-- 编辑弹框 -->
    <el-dialog title="编辑商品" :visible.sync="dialogVisible" width="50%">
      <el-form :model="saveobj" label-width="100px" class="demo-ruleForm">
        <el-form-item label="商品ID">
          <el-input v-model="saveobj.goods_id" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="saveobj.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input v-model="saveobj.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量">
          <el-input v-model="saveobj.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量">
          <el-input v-model="saveobj.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "goods",
  data() {
    return {
      dialogVisible: false,
      saveobj: {
        id: "",
        goods_name: "",
        goods_price: "",
        goods_number: "",
        goods_weight: "",
      },
      queryinfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      totalsize: 0, //记录总数据条数
      tableData: [], //表格数据源参数
    };
  },
  filters: {
    fmdtime: function (value) {
      const newdate = new Date(value);
      let str =
        newdate.getFullYear() +
        "-" +
        (newdate.getMonth() + 1) +
        "-" +
        newdate.getDate() +
        "<br/>" +
        newdate.getHours() +
        ":" +
        newdate.getMinutes() +
        ":" +
        newdate.getSeconds();
      return str;
    },
  },
  methods: {
    // 编辑
    async edit() {
      this.dialogVisible = false;
      let rst = await this.$http.put(`goods/${this.saveobj.goods_id}`, this.saveobj);
      if (rst.meta.status == 400) {
        this.$message.error(rst.meta.msg);
      }
    },
    async goeait(row) {
      this.dialogVisible = true;
      this.saveobj = { ...row };
    },
    // 删除
    del(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          //当点击确认时需要执行的代码块
          let rst = await this.$http.delete(`goods/${row.goods_id}`);
          if (rst.meta.status == 200) {
            this.query();
          } else {
            // 当删除失败时提示删除失败
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    // 点击添加按钮时触发的方法
    gosave() {
      // 跳转到添加页面
      this.$router.push("/home/addgoods");
    },
    // 当每页显示的数据条数发生变化时调用的方法,该方法接收一个参数,值为当前每页显示的数据条数
    handleSizeChange(size) {
      this.queryinfo.pagesize = size;
      this.query();
    },
    // 当点击页码时触发的方法,该方法接收一个参数,参数表示当前页码
    handleCurrentChange(pagenum) {
      this.queryinfo.pagenum = pagenum;
      this.query();
    },
    async query() {
      let rst = await this.$http.get("goods", this.queryinfo);
      if (rst.meta.status == 200) {
        // 获取到总数据条数,赋值给totalsize
        this.totalsize = rst.data.total;
        //获取商品列表信息赋值给表格数据源
        this.tableData = rst.data.goods;
      } else {
        this.$message.error("获取商品列表失败");
      }
    },
  },
  mounted() {
    // 调用查询数据的方法
    this.query();
  },
};
</script>

<style>
</style>
